<template>
	<view>
		<view v-if="islogin">
			<view class="head-container">
				<image :src="avatar" mode="aspectFill" class="img-avatar"></image>
				<view class="user-info-container">
					<view class="nick-type-container">
						<text class="txt-nick">{{nickName+"【"+uTypes[type]+"】"}}</text>
					</view>
				</view>
			</view>
			<text class="iconfont icon-ziyuanxhdpi txt-logout" @click="clickLogout"></text><!-- 注销 -->
			<view v-for="(item,index) in modules" :key="index" class="module-container" @click="clickGoPage(item.path)">
				<image :src="item.icon" mode="widthFix" class="module-icon"></image>
				<text class="module-name">{{item.name}}</text>
			</view>
		</view>
		<login-dialog ref="login" @after="afterLogin"></login-dialog>
	</view>
</template>

<script>
	import{pages_common,pages_student,pages_teacher,pages_admin }from'../../utils/pages.js'
	const app = getApp()
	export default {
		data() {
			return {
				islogin: app.globalData.token.length > 0,
				avatar: this.$params.host + app.globalData.avatar,
				nickName: app.globalData.nickName,
				uTypes: ["管理员", "教师", "学生"],
				type: app.globalData.type,
				modules: [],
			// 	oldValue: '',//1
			}
		},
		onLoad() {
			this.$noti.add(this.$params.noti_login_status, this.afterLogin, this)

		},
		onUnload() {
			this.$noti.remove(this.$params.noti_login_status, this)
		},
		mounted() {
			if (!this.islogin) {
				this.$refs.login.show()
			}
		},
		onShow() {
			if(!this.isLogin){
				return
			}
			// if (this.modules.length == 0 && this.authorized) {
			// 	let modules = [pages_admin, pages_teacher, pages_student]
			// 	this.modules = pages_common.concat(modules[this.type])
			// }
			this.loadData()//1
		},
		methods: {
			afterLogin(logout) {
				console.log(logout);
				if (logout || this.islogin) {
					
					return
				}
				// if (logout) {
				// 	this.$refs.login.show()
				// 	return
				// }
				// if (this.islogin) {
				// 	return
				// }
				this.islogin = app.globalData.token.length > 0
				this.avatar = this.$params.host + app.globalData.avatar
				this.nickName = app.globalData.nickName
				this.type = app.globalData.type
				// this.name = app.globalData.userName//1
				// this.email = app.globalData.email//1
				// this.$refs.login.hide()//1
				let modules = [pages_admin, pages_teacher, pages_student]
				this.modules = pages_common.concat(modules[this.type])
				// this.loadData()//1
			},
			// loadData(){
			// 	Page=0
			// 	this.modules=[]
			// },
			clickLogout() {
				let url=this.$params.host+this.$params.action_logout
				let data={
					token: app.globalData.token
				}
				this.$request.postParams(url,data,res=>{
					if(!res.success){
						console.log(res);
						uni.showToast({
							title:res.message,
							icon:'none'
						});
						this.$noti.post(this.$params.noti_login_status, true)
					}
					this.islogin = false
					app.globalData.avatar = '/static/icons/default_user.png'
					app.globalData.type = -1
					app.globalData.nickName = ''
					app.globalData.token = ''
					app.globalData.userName = ''
					// app.globalData.uid = ''
					app.globalData.email = ''
				    app.globalData.phone=""
					// this.islogin=false
					// this.$refs.login.show()
					this.$noti.post(this.$params.noti_login_status, true)
				})
				
			},
			clickGoPage(path){
				uni.navigateTo({
					// url:path,
					url: '../../components/login-dialog/login-dialog',
					     success: res => {
					      this.$refs.shell.clickToHide()
						  console.log(11111);
					     }
					    });
					console.log(12345);
				
			},
			clickToAdd() {
				this.$refs.addShell.showDialog()
			},
			// clickGoPage(id,name) {
			// 	this.oldValue = name
			// 	newName = name
			// 	updateId = id
			// 	this.$refs.updateShell.showDialog()
			// },
			
			fail: () => {
			
			},
			complete: () => {
			
			},
		}

		
	}
</script>

<style>
	@import url("index.css");

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
